import { apiLogin } from '@/api/user'
import { delToken, getToken, setToken } from '@/utils/storage'
import router from '@/router'

const state = {
    token: ''
}

const mutations = {
    setToken(state, token) {
        state.token = token
        setToken(token)
    },
    getToken(state) {
        const token = getToken()
        if (token) state.token = token
    },
    logout(state) {
        delToken()
        state.token = ''
        router.push('/login')
    }
}

const actions = {
    async getLogin(context, data) {
        const res = await apiLogin(data)
        context.commit('setToken', res.data.data.token)
    }
}

const getters = {

}

export default {
    namespaced: true,
    state,
    mutations,
    actions,
    getters
}
